<?php
/**
 * 左侧导航
 */
declare (strict_types = 1);

namespace app\admin\middleware;

use think\Facade\Cache;
use think\facade\View;

class Power
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //判定用户是否登录
        if($request->controller() != 'Login' && session('adminUserInfo') && $request->controller() != 'Webupload'){
            $handler = Cache::store('redis')->handler();

            $model = $request->controller();

            $powerData = $columnArr = $powerAllArr = [];
            if($handler->hget('columnRoleData', 'column_'.session('adminUserInfo.rid')) && $columnArr = json_decode($handler->hget('columnRoleData', 'column_'.session('adminUserInfo.rid')), true)){
                foreach($columnArr as $key=>$val){
                    if($val['children']){
                        foreach($val['children'] as $ck=>$cv){
                            if($cv['model'] == $model){
                                session('adminUserInfo.catId', $cv['id']);
                                session('adminUserInfo.model', $cv['model']);
                                $columnArr[$key]['active'] = true;
                                $columnArr[$key]['children'][$ck]['active'] = true;
                                break;
                            }else{
                                if($cv['children']){
                                    foreach($cv['children'] as $k=>$v){
                                        if($v['model'] == $model){
                                            session('adminUserInfo.model', $v['model']);
                                            $columnArr[$key]['active'] = true;
                                            $columnArr[$key]['children'][$ck]['active'] = true;
                                            $columnArr[$key]['children'][$ck]['children'][$k]['active'] = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //获取当前栏目下审核次数
            if($handler->hget('columnRoleData', 'column_0')){
            	foreach(json_decode($handler->hget('columnRoleData', 'column_0'), true) as $columnVal){
	                if($request->controller() == $columnVal['model']){
	                    foreach(array_reverse(explode(',', $columnVal['action_id'])) as $val){
	                        $actionInfo = $handler->hget('action', 'action_'.$val) ? json_decode($handler->hget('action', 'action_'.$val), true) : '';
	                        if($actionInfo && $actionInfo['action_num']){
	                            $totalExamineNum = $actionInfo['action_num'];
	                            break;
	                        }
	                    }
	                }
	            }
            }

            //获取角色对应增删改权限
            if(session('adminUserInfo.rid') == 1){
                //获取当前栏目可操作权限
                if($handler->hget('columnRoleData', 'column_0') && $powerAllArr = json_decode($handler->hget('columnRoleData', 'column_0'), true)){
                    foreach($powerAllArr as $value){
                        //当前栏目下存在可操作系统行为
                        if($value['action_id'] && $value['model'] == $request->controller()){
                            foreach(explode(',', $value['action_id']) as $val){
                                $actions = json_decode($handler->hget('action', 'action_-1'), true);
                                strstr($actions[$val], 'examine') ? '' : $powerData[$actions[$val]] = true;
                            }
                        }
                    }
                }else{
                    $powerData['add'] = $powerData['del'] = $powerData['edit'] = true;
                }
                //获取超管当前栏目下审核次数
                $examineNum = $totalExamineNum;
            }else{
                if($handler->hget('actionPowerData', 'actionPower_'.session('adminUserInfo.rid')) && $powerAllArr = json_decode($handler->hget('actionPowerData', 'actionPower_'.session('adminUserInfo.rid')), true)){
                    $examineNum = 0;
                    $actions = array_flip(json_decode($handler->hget('action', 'action_-1'), true));
                    foreach($powerAllArr as $key=>$val){
                        if($key == $model){
                            //获取用户当前栏目下操作权限
                            $powerData = $val;

                            //获取当前栏目下审核次数
                            foreach(array_reverse($val) as $k=>$v){
                                $actionInfo = json_decode($handler->hget('action', 'action_'.$actions[$k]), true);
                                if($actionInfo['action_num']){
                                    $examineNum = $actionInfo['action_num'];
                                    break;
                                }
                            }

                        }
                    }
                }
            }

            $request->examineNum = $examineNum;
            View::assign(['columnData'=>$columnArr, 'powerData'=>$powerData, 'examineNum'=>$examineNum, 'controller'=>$request->controller(), 'columnAction'=>json_decode($handler->hget('action', 'action_0'), true), 'totalExamineNum'=>$totalExamineNum]);
        }

        return $next($request);
    }

    public function getActiveColumn($columnArr, $k1 = null, $k2 = null){
        if(!$columnArr)  return false;
    }
}
